package eu.jba.denshijisho.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import eu.jba.denshijisho.R;
import eu.jba.denshijisho.entity.Stat;
import eu.jba.denshijisho.util.Util;

public class StatDao extends AbstractDao<Stat>{

	public static final String KEY_ROWID = "_id";
	
	public static final String KEY_NEXT_DATE = "nextDate";
	public static final String KEY_NEXT_DATE_NAME = "Next Date";
	public static final String KEY_PREVIOUS_DATE = "previousDate";
	public static final String KEY_PREVIOUS_DATE_NAME = "Previous Date";
	public static final String KEY_INTERVAL = "interval";
	public static final String KEY_INTERVAL_NAME = "Interval";
	public static final String KEY_REPS = "reps";
	public static final String KEY_REPS_NAME = "Reps";
	public static final String KEY_OK = "okAnswer";
	public static final String KEY_OK_NAME = "Ok Answer";
	public static final String KEY_WRONG = "wrongAnswer";
	public static final String KEY_WRONG_NAME = "Wrong Answer";
	public static final String KEY_EF = "ef";
	public static final String KEY_EF_NAME = "ef";
	
	public StatDao(Context ctx) {
		super(ctx);
	}

	@Override
	protected int getCreateResource() {
		return R.raw.create_table_stat;
	}

	@Override
	protected int getUpdateResource() {
		return R.raw.update_table_stat;
	}

	@Override
	protected String getTableName() {
		return getContext().getResources().getString(R.database.STAT_TABLE);
	}

	@Override
	protected ContentValues getInitialValues(Stat object) {
		final ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NEXT_DATE, Util.dateToString( object.getNextDate()));
		initialValues.put(KEY_PREVIOUS_DATE, Util.dateToString(object.getPreviousDate()));
		initialValues.put(KEY_INTERVAL, object.getInterval());
		initialValues.put(KEY_REPS, object.getReps());
		initialValues.put(KEY_OK, object.getOkAnswers());
		initialValues.put(KEY_WRONG, object.getWrongAnswers());
		initialValues.put(KEY_EF, object.getEf());
		return initialValues;
	}

	@Override
	protected String getRowIdColumnName() {
		return KEY_ROWID;
	}

	@Override
	protected String[] getColumns() {
		return new String[] {
			KEY_ROWID, KEY_NEXT_DATE, KEY_PREVIOUS_DATE, KEY_INTERVAL, KEY_REPS, KEY_OK, KEY_WRONG, KEY_EF
		};
	}

	@Override
	protected Stat convertTo(Cursor cursor) {
		final Stat stat = new Stat(
			cursor.getInt( 0 ), Util.stringToDate( cursor.getString( 1 ) ), 
			Util.stringToDate( cursor.getString( 2 ) ), cursor.getInt( 3 ), 
			cursor.getInt( 4 ), cursor.getInt( 5 ),  cursor.getInt( 6 ), 
			cursor.getDouble( 7 )
		);
		
		return stat;
	}

	public static String[] getColumnNames(){
		return new String[] {
			KEY_NEXT_DATE_NAME,KEY_PREVIOUS_DATE_NAME,KEY_INTERVAL_NAME,
			KEY_REPS_NAME,KEY_OK_NAME,KEY_WRONG_NAME,KEY_EF_NAME
		};
	}
	
	public Integer rawQuery( String query ){
		Cursor cursor = null;
		try {
			cursor = getDb().rawQuery(query, null);
			cursor.moveToFirst();
			int i = cursor.getInt(0);
			return i;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}

	}

}
